
<script type="text/javascript">
	$(document).ready(function(){
		
			//check all software
			$( "#all_sw" ).click(function(){
				var checked_status = this.checked;
					$("#software_div :checkbox").attr("checked",checked_status);
			});
			//check all hardware
			$( "#all_hw" ).click(function(){
				var checked_status = this.checked;
					$("#hardware_div :checkbox").attr("checked",checked_status);
			});

			//check room type radio button
			$( "#meetingroom" ).click(function(){
				if(this.checked){
					$("#all_sw").attr("disabled", true);
					$("#software_div :checkbox").attr("disabled", true);
					$("#comcount").attr("disabled", true );
				}
			});
			$("#labroom").click(function(){
				if(this.checked){
					$("#all_sw").removeAttr("disabled");
					$("#software_div :checkbox").removeAttr("disabled");
					$("#comcount").removeAttr("disabled");
				}
			});

			//search value
			$("#search_btn").click(function(){

				//check null values
				if($("#peoplecount").val().length==0 || $("#comcount").val().length==0 || $("#date").val().length==0){

					$("#peoplecount_err").show();
					$("#comcount_err").show();
					$("#date_err").show();

					if($("#peoplecount").val().length!=0){$("#peoplecount_err").hide();}
					if($("#comcount").val().length!=0){$("#comcount_err").hide();}
					if($("#date").val().length!=0){$("#date_err").hide();}
					
				}
				else{
					//hide error
					$("#peoplecount_err").hide();
					$("#comcount_err").hide();
					$("#date_err").hide();
					
					//get software checked value
					var software_checked = new Array();
					$("#software_div :input:checked").each(function(){
						software_checked[software_checked.length] = $(this).val();
					});
					//get hardware checked value
					var hardware_checked = new Array();
					$("#hardware_div :input:checked").each(function(){
						hardware_checked[hardware_checked.length] = $(this).val();
					});
					//sent data to search
					$.post("<?=base_url()?>main/search_room",{
						roomtype: $("input[name=roomtype]:checked").val(),
						peoplecount: $("#peoplecount").val(),
						comcount: $("#comcount").val(),
						date: $("#date").val(),
						software: software_checked,
						hardware: hardware_checked,
						timestart: $("#time_start").val(),
						timeend: $("#time_end").val()},
							function(result){
								$("#search_value").html(result);
								$("#search_value").slideDown(500);
							}
					);
					//$("#search_value").hide();
				}
			});

			//check num
			$("#peoplecount").blur(function(){
				if(isNaN($("#peoplecount").val())){
					$("#peoplecount").val("0");
				}
			});
			$("#comcount").blur(function(){
				if(isNaN($("#comcount").val())){
					$("#comcount").val("0");
				}
			});
			//end check num 
	
			//set jquery ui 
			$( "#tabs" ).tabs({collapsible: true});
			$( "#search_btn" ).button();
			
			$("#date").datepicker({ dateFormat: 'yy-mm-dd' });

			$("#peoplecount_err").hide();
			$("#comcount_err").hide();
			$("#date_err").hide();

			//hide dialog message 
			$("#dialog-message").hide();

			//set room radio selected 
			var len = $("input[name=room]").length;
			for(var i=0; i<len; i++)
			{
				if($("input[name=room]").eq(i).val() == '<?=$this->session->userdata('room')?>')
				{
					$("input[name=room]").eq(i).attr('checked', true);
				}
			}

			//check room radio calendar selected 
			$("input[name=room]").change(function(){
				$.post("<?=base_url()?>main/ajax_change_room", {
					room: $("input[name=room]:checked").val()
					},
						function(result){
							window.location='<?=base_url()?>main/index';
						}
				);
			});

			//get day from calendar 
			$(".day").click(function(){
				var day_num = $(this).find('.day_num').html();
				var month = '' +<?=$this->session->userdata('month')?>;
				var year = '' +<?=$this->session->userdata('year')?>;

				if(month.length == 1)
				{
					month = '0' +month;
				}
				if(day_num.length == 1)
				{
					day_num = '0' +day_num;
				}
				var date = "" +year +"-" +month +"-" +day_num;

				//set message in dialog 
				$.post("<?=base_url()?>reservation/showdetail",{
						day: date
					},
						function(result){
							$("#dialog-message").html(result);
							//show dialog 
							<?php 
								if($this->session->userdata('type')!=null)
								{
							?>
									$("#dialog-message").dialog({
										modal: true,
										buttons:{
											'จอง':function(){
												//call reservation form 
												$.post('<?=base_url()?>reservation/call_reservation_form',{
														data: '1'
													},
														function(result){
															$("#dialog-message").dialog("close");
															$("#reserve_dialog").html(result);
															$("#reserve_dialog").dialog({
																width: 400,
																modal: true,
																buttons:{
																	'จอง':function(){
																		//$(this).dialog("close");
																		$.post('<?=base_url()?>reservation/check_can_cannot_reserve',{
																			calendar_time_start: $("#calendar_time_start").val(),
																			calendar_time_end: $("#calendar_time_end").val()},
																				function(result){
																					if(result == 'N'){
																						alert('Can not');
																					}
																					else{
																						if(confirm('ยืนยันการจอง?') == true)
																						{
																							$( this ).dialog( "close" );
																							$("#reserve_form").submit();
																						}
																					}
																				}
																		);
																	},
																	'ยกเลิก':function(){
																		$(this).dialog("close");
																	}
																}
															});
														}
													);
											},
											'ยกเลิก':function(){
												$(this).dialog("close");
											}
										}
									});
							<?php		
								} else {
							?>
									$("#dialog-message").dialog({
										modal: true,
										buttons:{
											OK:function(){
												$(this).dialog("close");
											}
										}
									});
							<?php 
								}
							?>
							
						}
					);
			});

			
	});

	
	
</script>

			<!-- dialog box -->
			<div id="dialog-message" title="รายละเอียดการจอง"></div>
			<div id="reserve_dialog" title="จอง"></div>
			<!-- end dialog box -->
			
			<!-- Tabs -->
			<div id="tabs">
				<ul>
					<li><a href="#tabs-1">ปฏิทิน</a></li>
					<li><a href="#tabs-2">ค้นหาห้อง</a></li>
				</ul>
				<!-- tabs-1 ปฎิทิน -->
				<div id="tabs-1">
					<div>
						<table border="0">
							<tr>
								<td>ห้องปฎิบัติการทางคอมพิวเตอร์</td>
								<td>
								<?php 
									foreach ($room_lab as $row_lab) {
								?>
									<input type="radio" name="room" value="<?=$row_lab->roomname?>" /><?=$row_lab->roomname?>&nbsp;
								<?php
									}
								?>
								</td>
							</tr>
							<tr>
								<td>ห้องประชุม</td>
								<td>
								<?php 
									foreach ($room_meeting as $row_meeting)
									{
								?>
									<input type="radio" name="room" value="<?=$row_meeting->roomname?>" /><?=$row_meeting->roomname?>
								<?php
									}
								?>
								</td>
							</tr>
						</table>
					</div><br/>
					<div id="calendar">
						<?php echo $calendar; ?>
					</div>	
				</div>
				<!-- End tabs-1 -->
				
				<!-- tabs-2 ค้นหาห้อง -->
				<div id="tabs-2">
					<div id="search_form">
						<span><b>ประเภทห้อง</b></span><br/>
						<span>
							<input type="radio" id="labroom" name="roomtype" checked="checked" value="labroom" />ห้องปฎิบัติการทางคอมพิวเตอร์&nbsp;&nbsp;
							<input type="radio" id="meetingroom" name="roomtype" value="meetingroom" />ห้องประชุม
						</span><br/><br/>
						<span>
							<b>จำนวนคน</b>
							<input type="text" id="peoplecount" name="peoplecount" value="0" size="2" />
							<b>คน</b>
						</span>
						<span id="peoplecount_err" class="err">*กรุณากรอกจำนวนคน</span><br/><br/>
						<span>
							<b>จำนวนคอมพิวเตอร์ที่ต้องการใช้งาน</b>
							<input type="text" id="comcount" name="comcount" value="0" size="2" />
							<b>เครื่อง</b>
						</span>
						<span id="comcount_err" class="err">*กรุณากรอกจำนวนคอมพิวเตอร์</span><br/><br/>
						<span>
							<b>ปฏิทิน</b>
							<input type="text" id="date" name="date" size="12" />
							<span id="date_err" class="err">*กรุณากรอกวันที่</span>
						</span><br/><br/>
							<?php 
								if(isset($soft_all))
								{
									?>
									<span><b>ซอฟต์แวร์ที่ต้องการใช้</b><br/></span>
									<input type="checkbox" id="all_sw" />all software<br/>
							<div id="software_div">
									<?php 									
									foreach ($soft_all as $row)
									{
									?>
										<input type="checkbox" name="software" value="<?=$row->softname?>" /><?=$row->softname?><br/>
									<?php
									}
								}
							?>
							</div>
						<br/>
						
							<?php 
								if(isset($hard_all))
								{
									?>
									<span><b>ฮาร์ดแวร์ที่ต้องการใช้</b><br/></span>
									<input type="checkbox" id="all_hw" />all hardware<br/>
							<div id="hardware_div">
									<?php 								
									foreach ($hard_all as $row)
									{
									?>
										<input type="checkbox" name="hardware[]" value="<?=$row->hardname?>" /><?=$row->hardname?><br/>
									<?php
									}
								}
							?>
							</div>
						<br/>
						<span>
							<b>เวลา</b> 
							<select id="time_start" name="time_start">
								<option value="08:30">08:30</option>
								<option value="09:30">09:30</option>
								<option value="10:30">10:30</option>
								<option value="11:30">11:30</option>
								<option value="12:30">12:30</option>
								<option value="13:30">13:30</option>
								<option value="14:30">14:30</option>
								<option value="15:30">15:30</option>
							</select>
							<b>น.&nbsp;&nbsp;ถึง</b>
							<select id="time_end" name="time_end">
								<option value="09:30">09:30</option>
								<option value="10:30">10:30</option>
								<option value="11:30">11:30</option>
								<option value="12:30">12:30</option>
								<option value="13:30">13:30</option>
								<option value="14:30">14:30</option>
								<option value="15:30">15:30</option>
								<option value="16:30">16:30</option>
							</select>
							<b>น.</b>
						</span><br/><br/>
						<span id="search_btn_span">
							<input type="button" id="search_btn" name="search_btn" value="ค้นหา" />
						</span>
					</div>
					
					<br/><br/><br/>
					
					<div id="search_value"></div>
					
				</div>
				<!-- End tabs-2 -->
			</div>

		